#ifndef _PROTOCOL__
#define _PROTOCOL__

#include "main.h"
#include <stdbool.h>
#include <sqlite3.h>



#define LIGHT1    (0x0101)
#define MAX_BRIGHTNESS 100

#define Device_List_Table "sta_device"
#define Scene_List_Table "scene"


#define DWORD_BYTE_COMBINE(h1, h2, h3, h4)      (((uint32_t)(h1)<<24) + ((uint32_t)(h2)<<16) + ((uint32_t)(h3)<<8) + ((uint32_t)(h4)<<0))
#define DWORD_BYTE_DIVIDE_H1(x)                 ((uint8_t)((uint32_t)(x)>>24))
#define DWORD_BYTE_DIVIDE_H2(x)                 ((uint8_t)((uint32_t)(x)>>16))
#define DWORD_BYTE_DIVIDE_H3(x)                 ((uint8_t)((uint32_t)(x)>>8))
#define DWORD_BYTE_DIVIDE_H4(x)                 ((uint8_t)((uint32_t)(x)>>0))

#define DWORD_WORD_COMBINE(h1, h2)              (((uint32_t)(h1)<<16) + ((uint32_t)(h2)<<0))
#define DWORD_WORD_DIVIDE_H1(x)                 ((uint16_t)((uint32_t)(x)>>16))
#define DWORD_WORD_DIVIDE_H2(x)                 ((uint16_t)((uint32_t)(x)>>0))

//M_DWORD_WORD_COMBINE(0x30, 0x00) 的结果为 0x3000
#define M_DWORD_WORD_COMBINE(byte1, byte0) ((uint16_t)(((uint8_t)(byte1) << 8) | (uint8_t)(byte0)))



typedef enum
{
    HearBeat_Request        = 0x0300,   //【心跳请求】
    HearBeat_Response       = 0x0400,   //【心跳响应】
    URL_Request             = 0x0500,   //【配置URL请求】
    URL_Response            = 0x0600,   //【配置URL请求】
    Park_Area_Sensor        = 0x0700,   //【设置停车场感应器】
    Short_Address_Contral   = 0x0900,   //【短地址控制】
    Data_Report             = 0x0F00,   //【数据下发请求，需要开始解析这串数据】
    Device_Status_Report    = 0x1100,   //【主动触发数据上报】
}_BroadLink_Protocol;

//================================

// typedef struct three_bright_setting
// {
//     uint8_t CF_RadarOnOff;
//     uint8_t CF_BrightH;
//     uint16_t CF_BrightHDelay;
//     uint8_t CF_BrightM;
//     uint16_t CF_BrightMDelay;
//     uint8_t CF_BrightL;
// }Three_Bright_Setting;


//================================

typedef struct Device {
    char did[64];
    char pid[64];
    int devicetypeflag;
    char blecatetory[64];
    char spid[64];
    char devname[64];
    char room[64];
    int roomId;
    char bletoken[64];
    char aeskey[64];
    int terminalid;
    int ignoreflag;
    char version[64];
    char extendInfo[64];
    char shortaddr[10];
    char sdid[64];
    bool CF_OnOff;

    uint8_t CF_RadarOnOff;
    uint8_t CF_BrightH;
    uint16_t CF_BrightHDelay;
    uint8_t CF_BrightM;
    uint16_t CF_BrightMDelay;
    uint8_t CF_BrightL;

} Device;

//================================

typedef struct scene 
{
    char moduledev_sdid[20];
    char moduledev_content[64];
    char moduledev_did[64];
    char name[12];
    int roomId;
    char image[64];
    int backColor;
    char room[12];
    char bletoken[12];
    char moduleid[12];
    bool ignoreflag;
} Scene;

//================================
typedef struct {
    int roomId;
    char roomName[20];
} Room;

typedef struct familyInfo 
{
    Room *roomlist_array;
    int roomlist_array_count;
    char name[20];
    int updateTime;
    char familyId[20];
} FamilyInfo;

//================================



extern void FSL_Handle_Protocol_Data(uint16_t Type, uint16_t Seq, struct sockaddr_in* client_addr, char *buf);
extern void FSL_HearBeat_Response(uint16_t Seq, struct sockaddr_in* client_addr);
extern void FSL_Short_Adress_Control(uint16_t Seq, struct sockaddr_in* client_addr, uint16_t short_adress, bool on_off, uint8_t brightness);
extern void FSL_URL_Request(uint16_t  Seq, struct sockaddr_in* client_addr);
extern int FSL_URL_Response(char *buf);
extern void FSL_Set_Park_Area_Sensor(uint16_t Seq, struct sockaddr_in* client_addr, uint16_t short_adress, 
                                uint8_t if_Group_control, char Group_id, uint8_t Sensor_Switch, 
                                uint8_t Brightness_H, uint16_t CF_BrightHDelay, uint8_t Brightness_Lowest,  
                                uint8_t Brightness_Middle, uint16_t Brightness_Middle_Delay_time);
extern void FSL_Data_Report(char *buf);



extern uint16_t sqlite3_check_table_columns(sqlite3 *db, const char *table_name);
extern void sqlite3_open_database(sqlite3 **db);
void sqlite3_create_table(sqlite3 *db);


#endif
